<?php
/**
 * 一次性脚本：批量开通会员
 * 用法：浏览器访问
 *   http://你的域名/update_vip.php?vip_time=2025-12-31 23:59:59
 * 会把所有用户的“是否会员”设为“真”，并把“会员时间”设为“2025-12-31 23:59:59”
 * 改完立即生效，用完可删除本文件
 */

header('Content-Type: application/json; charset=utf-8');

// 1. 引入数据库配置
$dbConfig = include('db_config.php');

// 2. 获取会员到期时间（必填）
$vipTime = isset($_GET['vip_time']) ? trim($_GET['vip_time']) : '';
if ($vipTime === '') {
    http_response_code(400);
    echo json_encode([
        'status'  => 'error',
        'message' => '缺少参数：vip_time'
    ], JSON_UNESCAPED_UNICODE);
    exit;
}

try {
    // 3. 建立 PDO 连接
    $dsn = "mysql:host={$dbConfig['host']};dbname={$dbConfig['dbname']};charset={$dbConfig['charset']}";
    $pdo = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 4. 执行更新
    $stmt = $pdo->prepare("
        UPDATE mi_users
        SET 是否会员 = '真',
            会员时间 = :vipTime
    ");
    $stmt->execute([':vipTime' => $vipTime]);

    // 5. 返回结果
    echo json_encode([
        'status'  => 'success',
        'message' => '已批量开通会员，到期时间：' . $vipTime,
        'rows'    => $stmt->rowCount()
    ], JSON_UNESCAPED_UNICODE);

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode([
        'status'  => 'error',
        'message' => '数据库操作失败：' . $e->getMessage()
    ], JSON_UNESCAPED_UNICODE);
}
?>
